<?php

namespace App\ZhCN\Api;

if (!defined('IN_PX'))
    exit;

use App\Service;
use App\Repository;
use Phoenix\Support\MsgHelper;
use Phoenix\Log\Log4p as logger;

class ContactForm {

    private function __RestController() {}

    private function __Route($value = '/api') {}

    protected function __Inject($session, Repository\ContactForm $contactForm, Service\Mail $servMail) {}

    /**
     * 首页联系表单接品
     * @return array
     */
    public function form() {
        $_hash = $this->session->contactHash['hash'];
        $_submitTime = $this->session->contactHash['time'];

        if ($_hash == $_POST['hash'] && $_submitTime == 0) {
            if (isset($_POST)) {
                $contactForm = json_encode($_POST);
                if ($flag = $this->contactForm->save($contactForm)) {
                    $title = '用户' . $_POST['name'] . '提交的表单';
                    $content = '用户名: ' . $_POST['name'] . '<br/>手机号: ' . $_POST['phone'] . '<br/>邮箱: ' . $_POST['email'] . '<br/>公司: ' . $_POST['company'] . '<br/>职位: ' . $_POST['position'] . '<br/>主题: ' . $_POST['subject'] . '<br/>内容: ' . $_POST['content'];
                    $result = $this->servMail->send('cindys@taihengco.com', $title, $content);
//                    $result = $this->servMail->send('455224159@qq.com', $title, $content);
                    $this->session->contactHash = array('time' => 1);
                    return MsgHelper::err(0);
                } else {
                    return  MsgHelper::err(10003, '系统繁忙!');
                }
            } else {
                return MsgHelper::err(10002, '非法操作!');
            }
        } else {
            return MsgHelper::err(10011, '不能重复提交!');
        }
    }

    public function enform() {
        $_hash = $this->session->contactHash['hash'];
        $_submitTime = $this->session->contactHash['time'];

        if ($_hash == $_POST['hash'] && $_submitTime == 0) {
            if (isset($_POST)) {
                $contactForm = json_encode($_POST);
                if ($flag = $this->contactForm->save($contactForm)) {
                    $title = '用户' . $_POST['name'] . '提交的表单';
                    $content = '用户名: ' . $_POST['name'] . '<br/>手机号: ' . $_POST['phone'] . '<br/>邮箱: ' . $_POST['email'] . '<br/>公司: ' . $_POST['company'] . '<br/>职位: ' . $_POST['position'] . '<br/>主题: ' . $_POST['subject'] . '<br/>内容: ' . $_POST['content'];
                    $result = $this->servMail->send('cindys@taihengco.com', $title, $content);
//                    $result = $this->servMail->send('455224159@qq.com', $title, $content);
                    $this->session->contactHash = array('time' => 1);
                    return MsgHelper::err(0);
                } else {
                    return  MsgHelper::err(10003, 'Busy system!');
                }
            } else {
                return MsgHelper::err(10002, 'Illegal operation!');
            }
        } else {
            return MsgHelper::err(10011, 'Cannot resubmit!');
        }
    }

    public function email() {
        $_hash = $this->session->contactHash['hash'];
        $_submitTime = $this->session->contactHash['time'];
        if ($_hash == $_POST['hash'] && $_submitTime == 0) {
            if (isset($_POST)) {
                //检查是否已经订阅过
                if ($this->contactForm->fingEmail($_POST['email'])) {
                    return MsgHelper::err(20000, '您已经订阅过最新资讯!');
                }
                if ($flag = $this->contactForm->saveEmail($_POST['email'])) {
                    $this->session->contactHash = array('time' => 1);
                    return MsgHelper::err(0);
                } else {
                    return  MsgHelper::err(10003, '系统繁忙!');
                }
            } else {
                return MsgHelper::err(10002, '非法操作!');
            }
        } else {
            return MsgHelper::err(10011, '不能重复提交!');
        }
    }

    public function enEmail() {
        $_hash = $this->session->contactHash['hash'];
        $_submitTime = $this->session->contactHash['time'];
        if ($_hash == $_POST['hash'] && $_submitTime == 0) {
            if (isset($_POST)) {
                //检查是否已经订阅过
                if ($this->contactForm->fingEmail($_POST['email'])) {
                    return MsgHelper::err(20000, 'You have already subscribed to the latest information!');
                }
                if ($flag = $this->contactForm->saveEmail($_POST['email'])) {
                    $this->session->contactHash = array('time' => 1);
                    return MsgHelper::err(0);
                } else {
                    return  MsgHelper::err(20000, 'Busy system!');
                }
            } else {
                return MsgHelper::err(20000, 'Illegal operation!');
            }
        } else {
            return MsgHelper::err(20000, 'Unable to repeat submission!');
        }
    }
}
